<?php

namespace app\modules\Order\models;

use Yii;
use app\modules\Product\models\ProductRoom;
use dektrium\user\models\User;

/**
 * This is the model class for table "order_refund".
 *
 * @property integer $id
 * @property integer $product_id
 * @property integer $order_id
 * @property integer $order_room_id
 * @property string $room_price
 * @property string $price
 * @property string $desc
 * @property integer $user_id
 * @property integer $examine_id
 * @property string $params
 * @property integer $status
 * @property string $created_at
 * @property string $check_at
 * @property string $updated_at
 */
class OrderRefund extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_refund';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['product_id', 'order_id', 'order_room_id'], 'required'],
            [['product_id', 'order_id', 'order_room_id', 'user_id', 'examine_id', 'status'], 'integer'],
            [['room_price', 'price'], 'number'],
            [['created_at', 'check_at', 'updated_at'], 'safe'],
            [['desc'], 'string', 'max' => 255],
            [['params'], 'string', 'max' => 1024],
        ];
    }


	//  获取取消舱房对应的订单
	public function getOrder(){
		
        return $this->hasOne(Order::className(), ['id' => 'order_id']);
	}

	public function getRoom(){//{{{

		$order = Order::findOne($this->order_id);
		$orderRoom = json_decode($this->params, true);

		if($order->product->stock == 1){  // 标准库存格式
			$productRoom  = ProductRoom::findOne($orderRoom['room_id']);

			return $productRoom->room->name .' '. $productRoom->room->code;

		}elseif($order->product->stock == 2){	// 自定义库存格式

			$room = $order->product->rooms;

			if($room){

				$arr = json_decode($room, true);
				return $arr[$orderRoom['room_id']]['name'];
			}else{

				return "无法获取";
			}
		}
	}//}}}

    //  获取订单对应录入人
    public function getMember(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app', 'Product ID'),
            'order_id' => Yii::t('app', 'Order ID'),
            'order_room_id' => Yii::t('app', 'Order Room ID'),
            'room_price' => Yii::t('app', 'Room Price'),
            'price' => Yii::t('app', 'Price'),
            'desc' => Yii::t('app', 'Desc'),
            'user_id' => Yii::t('app', 'User ID'),
            'examine_id' => Yii::t('app', 'Examine ID'),
            'params' => Yii::t('app', 'Params'),
            'status' => Yii::t('app', 'Status'),
            'created_at' => Yii::t('app', 'Created At'),
            'check_at' => Yii::t('app', 'Check At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
